package com.achobeta.recruitment.component.interceptor;

import com.achobeta.recruitment.common.AppResult;
import com.achobeta.recruitment.common.ResultCode;
import com.achobeta.recruitment.utils.JsonUtils;
import com.achobeta.recruitment.utils.SessionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-27
 * Time: 0:59
 */
@Slf4j
@Component
public class ManagerInterceptor implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //业务逻辑
        if (SessionUtils.getManager(request) != null) {
            return true;//用户登录
        }
        log.warn(ResultCode.FAILED_FORBIDDEN.toString() + " 已拦截：" + request.getRequestURI());
        response.setContentType("application/json; charset=utf8");
        //判断路径是否正确
        response.getWriter().write(JsonUtils.objectToJson(AppResult.redirect()));
        return false;
    }

}
